package com.chixing.controller;

import com.chixing.pojo.Customer;
import com.chixing.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wtl
 * @Date: 2021/10/05/15:23
 * @Description:
 */
@Controller
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @PostMapping("cust/login")
    public ModelAndView login(Long custTelno, String custPassword, HttpSession session){
        ModelAndView mav = new ModelAndView();
        Customer customer = customerService.login(custTelno,custPassword);
        if(customer!=null){
            session.setAttribute("loginCustomer",customer);
            mav.setViewName("index");
        }else {
            mav.addObject("loginMsg","用户名或密码是错误的,请重新输入");
            mav.setViewName("customer/login");
        }
        return mav;
    }
    @GetMapping("cust/logut")
    public String logut(HttpSession session){
        session.removeAttribute("loginCustomer");
        return "redirect:/";  //重定向
    }

}
